<?php
// @formatter:off
/**
 * @file c.search.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.search.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/themes/default/controlers/contents/c.search.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_SPECIAL_SEARCH)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcGroup;
	global $wcSearch;
	global $wcDefaults;
	global $wctDefaults;

	$wcThemeAssigns["PATH"] = "Search";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.search.html";

	$wcThemeAssigns["search-items"] = array();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minSrchInPage = $wctDefaults["max-per-page"]["search"] * ($page - 1);
	$maxSrchInPage = $minSrchInPage + $wctDefaults["max-per-page"]["search"];

	$count = 0;
	if(isset($_REQUEST["pattern"])) {
		$onlyTypes = array();
		$pattern = WCSearchFactory::StringToPatterns(turldecode($_REQUEST["pattern"]));
		$wcThemeAssigns["search-pattern"] = implode(" ", $pattern);
		$exceptions = WCSearchFactory::StringToPatterns(turldecode(@$_REQUEST["exceptions"]));
		$wcThemeAssigns["search-exceptions"] = implode(" ", $exceptions);
		$wcThemeAssigns["PATH"] .= ": {$wcThemeAssigns["search-pattern"]}";

		foreach(WCSearchFactory::$TypePriority as $type => $rank) {
			if(isset($_REQUEST["O-{$type}"])) {
				$wcThemeAssigns["O-{$type}"] = true;
				$onlyTypes[] = $type;
			}
		}

		$result = $wcSearch->get($pattern, $wcGroup, true, $onlyTypes, $exceptions);
		$result_count = count($result);
		$wcThemeAssigns["result-count"] = $result_count;

		if($result_count) {
			$ids = 1;
			foreach($result as $item) {
				if($count >= $minSrchInPage && $count < $maxSrchInPage) {
					$aux = array(
						"name" => $item->name(),
						"full-name" => $item->fullName(),
						"factor" => $item->factor(),
						"hits" => $item->hits(),
						"link" => $item->link(),
						"type" => $item->type(),
						"id" => $ids++
					);
					if($item->hasPreviews()) {
						$aux["previews"] = $item->previews();
						$aux["preview"] = isset($aux["previews"][0]) ? $aux["previews"][0] : $wcDefaults['preview'];
					}
					if($item->count() !== false) {
						$aux["count"] = $item->count();
					}

					$wcThemeAssigns["search-items"][] = $aux;
				}
				$count++;
			}
		}

		$wcThemeAssigns["time-elapsed"] = $wcSearch->elapsedTime();
		$wcThemeAssigns["time-search"] = $wcSearch->searchTime();
		$wcThemeAssigns["time-secondlayer"] = $wcSearch->secondLayerTime();
		$wcThemeAssigns["time-build"] = $wcSearch->buildTime();
		if($exceptions) {
			$wcThemeAssigns["time-filter"] = $wcSearch->filteringTime();
		}

		$wcThemeAssigns["SEARCH-WORDS"] = $pattern;

		wcSetPager(requestParams(true, $wcAction, array('page')), ceil($result_count / $wctDefaults["max-per-page"]["search"]), $page);
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to search items";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>